/**
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */

#import "AppDelegate.h"

#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#import "YYConst.h"


@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  
  // 申请通知权限
  if (CURRENT_IOS_VERSION >= 10.0) {
    [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound) completionHandler:^(BOOL granted, NSError * _Nullable error) {
      if (granted) {
        NSLog(@"已经获取通知权限");
      }
    }];
  }
  
  // 注册通知
  [UNUserNotificationCenter currentNotificationCenter].delegate = self.notificationHandler;
  
  // 设置鹰眼SDK的基础信息
  // 每次调用startService开启轨迹服务之前，可以重新设置这些信息。
  BTKServiceOption *basicInfoOption = [[BTKServiceOption alloc] initWithAK:AK mcode:MCODE serviceID:serviceID keepAlive:FALSE];
  [[BTKAction sharedInstance] initInfo:basicInfoOption];
  
  // 初始化地图SDK
  BMKMapManager *mapManager = [[BMKMapManager alloc] init];
  [mapManager start:AK generalDelegate:self];
  
    NSURL *jsCodeLocation;

  jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];

  RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                      moduleName:@"DPProject"
                                               initialProperties:nil
                                                   launchOptions:launchOptions];
  rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];

  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  UIViewController *rootViewController = [UIViewController new];
  rootViewController.view = rootView;
  self.window.rootViewController = rootViewController;
  [self.window makeKeyAndVisible];
  return YES;
}
#pragma mark - BMKGeneralDelegate
-(void)onGetNetworkState:(int)iError {
  if (0 == iError) {
    NSLog(@"联网成功");
  } else{
    NSLog(@"onGetNetworkState %d",iError);
  }
}

- (void)onGetPermissionState:(int)iError {
  if (0 == iError) {
    NSLog(@"授权成功");
  } else {
    NSLog(@"onGetPermissionState %d",iError);
  }
}
-(void)applicationDidBecomeActive:(UIApplication *)application {
  // 每次进入应用时将角标清零
  [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}
#pragma mark - setter & getter
-(YYNotificationHandler *)notificationHandler {
  if (_notificationHandler == nil) {
    _notificationHandler = [[YYNotificationHandler alloc] init];
  }
  return _notificationHandler;
}

@end
